<!DOCTYPE html>
<html>

<head>
    <title>比赛信息 - 答题系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #2196F3;
            --secondary-color: #1976D2;
            --background: #f8f9fa;
            --surface: #ffffff;
            --text-primary: #212121;
            --text-secondary: #757575;
        }

        html,
        body {
            height: 100%;
            margin: 0;
            overflow: hidden;
            font-family: 'Roboto', sans-serif;
        }

        .container {
            height: 100vh;
            display: flex;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
        }

        /* 左侧内容 */
        .content {
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 40px;
            color: white;
            min-width: 600px;
        }

        /* 右侧玩家列表容器 */
        .player-list-container {
            width: 320px;
            background: rgba(255, 255, 255, 0.98);
            padding: 30px 25px;
            box-shadow: -4px 0 24px rgba(0, 0, 0, 0.08);
            backdrop-filter: blur(6px);
            border-left: 1px solid rgba(255, 255, 255, 0.3);
            overflow-y: auto;
        }

        /* 自定义滚动条 */
        .player-list-container::-webkit-scrollbar {
            width: 8px;
        }

        .player-list-container::-webkit-scrollbar-track {
            background: rgba(0, 0, 0, 0.05);
        }

        .player-list-container::-webkit-scrollbar-thumb {
            background: rgba(var(--primary-color), 0.3);
            border-radius: 4px;
        }

        /* 列表标题 */
        .player-list-title {
            font-size: 1.8rem;
            color: var(--text-primary);
            margin-bottom: 1.8rem;
            padding-bottom: 1rem;
            border-bottom: 2px solid rgba(var(--primary-color), 0.15);
            position: relative;
        }

        .player-list-title::after {
            content: "";
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 60px;
            height: 2px;
            background: var(--primary-color);
        }

        /* 玩家条目优化 */
        .player-item {
            font-size: 1.1rem;
            color: var(--text-primary);
            padding: 14px 20px;
            margin: 8px 0;
            background: rgba(255, 255, 255, 0.9);
            border-radius: 10px;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.03);
            transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
            display: flex;
            align-items: center;
            position: relative;
        }

        /* 添加序号装饰 */
        .player-item::before {
            content: attr(data-index);
            display: inline-block;
            width: 24px;
            height: 24px;
            background: rgba(var(--primary-color), 0.1);
            color: var(--primary-color);
            border-radius: 6px;
            text-align: center;
            line-height: 24px;
            font-weight: 500;
            margin-right: 16px;
            flex-shrink: 0;
        }

        /* 悬停效果 */
        .player-item:hover {
            background: rgba(var(--primary-color), 0.06);
            transform: translateX(6px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        }

        /* 文字截断优化 */
        .player-item {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: 100%;
        }

        /* 入场动画 */
        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateX(20px);
            }

            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        /* 玩家数量徽章 */
        .players-count {
            position: absolute;
            right: 30px;
            top: 30px;
            background: var(--primary-color);
            color: white;
            width: 32px;
            height: 32px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);
        }

        /* 原有样式优化 */
        .quiz-title {
            font-size: 3.5rem;
            font-weight: 600;
            margin-bottom: 2rem;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
            text-align: center;
        }

        .invite-code {
            font-size: 10rem;
            letter-spacing: 12px;
            font-weight: 800;
            background: linear-gradient(45deg, #fff, #e0f2fe);
            -webkit-background-clip: text;
            background-clip: text;
            -webkit-text-fill-color: transparent;
            margin: 2rem 0;
            text-align: center;
        }

        .action-button {
            padding: 1.2rem 3rem;
            font-size: 1.3rem;
            background: rgba(255, 255, 255, 0.9);
            border: none;
            border-radius: 50px;
            color: var(--primary-color);
            cursor: pointer;
            transition: transform 0.2s;
            margin-top: 2rem;
        }

        .action-button:hover {
            transform: scale(1.05);
        }
    </style>
    <script src="/basic.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.2.1/css/all.min.css">
</head>

<body>
    <div class="container">
        <div class="content">
            <h1 class="quiz-title" id="quizTitle">知识竞赛</h1>
            <div class="invite-code" id="inviteCode"><i class="fa-solid fa-hourglass-half"></i> 加载中...</div>
            <button class="action-button" onclick="nextPage()"><i class="fa-brands fa-readme"></i> 阅读比赛须知</button>
        </div>

        <div class="player-list-container">
            <div class="players-count" id="playersCount">0</div>
            <h2 class="player-list-title">已加入玩家</h2>
            <ul class="player-list" id="playerList"></ul>
        </div>
    </div>

    <script src="/socket.io/socket.io.js"></script>
    <script>
        const urlParams = new URLSearchParams(window.location.search);
        const quizId = urlParams.get('id');
        const socket = io();
        const playerList = document.getElementById('playerList');
        const playerMap = new Object();
        let playerCount = 0;

        async function loadQuizInfo() {
            try {
                const response = await fetch(`/quiz/${quizId}`);
                const { data } = await response.json();
                document.getElementById('quizTitle').textContent = data.name;
                document.title = data.name;
                document.getElementById('inviteCode').textContent = data.invite_code;
            } catch (error) {
                console.error('加载失败:', error);
            }
        }

        function nextPage() {
            window.location.href = `notice?id=${quizId}`;
        }

        function setupSocket() {
            socket.on('players-update', player => {
                if (playerMap[player.name]) return;
                const li = document.createElement('li');
                li.className = 'player-item';
                li.setAttribute('data-index', ++playerCount);
                li.textContent = player.name;
                li.style.animation = 'slideIn 0.3s ease-out';
                playerMap[player.name] = li;
                playerList.appendChild(li);

                // 更新数量徽章
                document.getElementById('playersCount').textContent = playerCount;
            });

            socket.emit('info-join', { quizId });
        }

        document.addEventListener('DOMContentLoaded', () => {
            loadQuizInfo();
            playerList.innerHTML = '';
            setupSocket();
        });
    </script>
</body>

</html>